package com.htfyun.ft.fragment.factory;

import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.htfyun.ft.R;

import androidx.annotation.Nullable;

public class FragmentAccelerateSensor extends FactoryBaseFragment {

    private TextView txtValue;

    private SensorManager sensorManager;
    private SensorEventListener sensorEventListener;
    private Sensor accelerateSensor;

    private static final float DefaultShakeThreshold = (float) 200.0;

    /**
     * 检测的时间间隔
     */
    private static final int UPDATE_INTERVAL = 100;
    /**
     * 上一次检测的时间
     */
    private long mLastUpdateTime;
    /**
     * 上一次检测时，加速度在x、y、z方向上的分量，用于和当前加速度比较求差。
     */
    private float mLastX, mLastY, mLastZ;

    private Context mContext;

    @Override
    protected int getLayoutRes() {
        return R.layout.fragment_factory_accelerate_sensor;
    }

    @Override
    protected void onInitView(@Nullable Bundle savedInstanceState) {
        super.onInitView(savedInstanceState);

        mContext = getContext();



        txtValue = (TextView) findViewById(R.id.txtValue);

        sensorManager = mContext.getSystemService(SensorManager.class);
        accelerateSensor = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);

        sensorEventListener = new MySensorListener();

        setBtnPassVisibility(View.GONE);

        TextView txtTips = (TextView) findViewById(R.id.txtTips);
        txtTips.setText(getString(R.string.test_accelerate_sensor_tips, DefaultShakeThreshold));

    }


    @Override
    protected void onVisible() {
        super.onVisible();

        sensorManager.registerListener(sensorEventListener, accelerateSensor, SensorManager.SENSOR_DELAY_NORMAL);
    }

    @Override
    protected void onInvisible() {
        super.onInvisible();

        sensorManager.unregisterListener(sensorEventListener);

    }

    private final class MySensorListener implements SensorEventListener {

        @Override
        public void onSensorChanged(SensorEvent event) {
            if (event.sensor.getType() != Sensor.TYPE_ACCELEROMETER) {
                return;
            }
            long currentTime = System.currentTimeMillis();
            long diffTime = currentTime - mLastUpdateTime;
            if (diffTime < UPDATE_INTERVAL) {
                return;
            }

            mLastUpdateTime = currentTime;
            float x = event.values[SensorManager.DATA_X];
            float y = event.values[SensorManager.DATA_Y];
            float z = event.values[SensorManager.DATA_Z];

            float deltaX = x - mLastX;
            float deltaY = y - mLastY;
            float deltaZ = z - mLastZ;
            mLastX = x;
            mLastY = y;
            mLastZ = z;
            float delta = (float) Math.sqrt(deltaX * deltaX + deltaY * deltaY + deltaZ * deltaZ) / diffTime * 10000;
            double delta2 = Math.sqrt(deltaX * deltaX + deltaY * deltaY + deltaZ * deltaZ);

            txtValue.setText(getString(R.string.test_accelerate_sensor_value, x, y, z, delta));
            if (delta > DefaultShakeThreshold) {
                delayAutoPass();
            }
        }

        @Override
        public void onAccuracyChanged(Sensor sensor, int accuracy) {

        }
    }
}
